package com.tarzan.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tarzan.api.model.request.user.InserUserInfoReq;
import com.tarzan.api.model.request.user.SysUserIdReq;
import com.tarzan.api.model.request.user.SysUserInfoReq;
import com.tarzan.api.model.request.user.UpdateUserInfoReq;
import com.tarzan.api.model.response.user.SysUserDetailInfoResp;
import com.tarzan.api.model.response.user.SysUserInfoResp;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.system.entity.SysUser;
import com.tarzan.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liujinshan
 * @title: SysUserController
 * @projectName tarzan-cloud
 * @description: TODO 用户信息管理类
 * @date 2021-5-19 21:39
 */
@RestController
@RequestMapping("user")
public class SysUserController {


    private final static Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private ISysUserService iSysUserService;


    /**
     *  根据id 查询对象信息
     * @param sysUserIdReq
     * @return
     */
    @PostMapping(value = "/getUserInfoById" ,produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<SysUserInfoResp> getUserInfoById(@RequestBody  SysUserIdReq sysUserIdReq){
        BaseResponse<SysUserInfoResp> response = new BaseResponse<>();
        try {
            response = BaseResponse.ok(iSysUserService.getUserInfoById(sysUserIdReq));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.info("SysUserController -> getUserInfoById() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("99999");
            response.setErrMsg(e.getMessage());
            logger.error("SysUserController -> getUserInfoById() Exception e="+e);
        }
        return response;
    }

    /**
     * @title findUserInfoByUserName
     * @description TODO 根据用户名称查询用户信息
     * @param username
     * @return com.tarzan.common.core.base.BaseResponse<com.tarzan.api.model.response.user.SysUserInfoResp>
     * @author liujinshan
     * @data 2021/7/1 15:51
     */
    @GetMapping(value = "/findUserInfoByUserName/{username}")
    public BaseResponse<SysUserDetailInfoResp> findUserInfoByUserName(@PathVariable String username){
        BaseResponse<SysUserDetailInfoResp> response = new BaseResponse<>();
        try {
            response = BaseResponse.ok(iSysUserService.findUserInfoByUserName(username));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.info("SysUserController -> findUserInfoByUserName() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("99999");
            response.setErrMsg(e.getMessage());
            logger.error("SysUserController -> findUserInfoByUserName() Exception e="+e);
        }
        return response;
    }

    /**
     * 查询分页用户信息
     * @param sysUserInfoReq
     * @return
     */
/*    @PreAuthorize("hasPermission('$everyone','system:user:list')")*/
    @PostMapping(value = "/getPageUserInfo" ,produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<IPage<SysUserInfoResp>> getPageUserInfo(@RequestBody SysUserInfoReq sysUserInfoReq){
        BaseResponse<IPage<SysUserInfoResp>> response = new BaseResponse<>();
        try {
            response = BaseResponse.ok(iSysUserService.getPageUserInfo(sysUserInfoReq));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.info("SysUserController -> getPageUserInfo() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("99999");
            response.setErrMsg(e.getMessage());
            logger.error("SysUserController -> getPageUserInfo() Exception e="+e);
        }
        return response;
    }


    /**
     * 新增用户
     * @param inserUserInfoReq
     * @param result
     * @return
     */
/*    @PreAuthorize("hasPermission('$everyone','system:user:add')")*/
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse add(@RequestBody @Validated InserUserInfoReq inserUserInfoReq, BindingResult result){
        try {
/*            inserUserInfoReq.setPassword(HappyPasswordEncoder.passwordEncryption(inserUserInfoReq.getPassword()));*/
            iSysUserService.insert(inserUserInfoReq);
        } catch (BusinessException e) {
            logger.info("SysUserController -> add() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysUserController -> add() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * 修改用户信息
     * @param updateUserInfoReq
     * @return
     */
/*    @PreAuthorize("hasPermission('$everyone','system:user:edit')")*/
    @PostMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody UpdateUserInfoReq updateUserInfoReq){
        try {
            iSysUserService.update(updateUserInfoReq);
        } catch (BusinessException e) {
            logger.info("SysUserController -> update() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysUserController -> update() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 删除用户信息
     * @param sysUserIdReq
     * @return
     */
/*    @PreAuthorize("hasPermission('$everyone','system:user:remove')")*/
    @PostMapping(value = "delete",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse delete(@RequestBody  SysUserIdReq sysUserIdReq){
        try {
            iSysUserService.delete(sysUserIdReq);
        } catch (BusinessException e) {
            logger.info("SysUserController -> delete() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysUserController -> delete() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 删除用户信息
     * @param ids
     * @return
     */
/*    @PreAuthorize("hasPermission('$everyone','system:user:remove')")*/
    @PostMapping(value = "deleteBatchByIds",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse deleteBatchByIds(@RequestBody List ids){
        try {
            iSysUserService.deleteBatchByIds(ids);
        } catch (BusinessException e) {
            logger.info("SysUserController -> deleteBatchByIds() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysUserController -> deleteBatchByIds() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 重置密码
     * @param sysUserIdReq
     * @return
     */
/*    @PreAuthorize("hasPermission('$everyone','system:user:resetPwd')")*/
    @PostMapping(value = "resetPassword",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse resetPassword(@RequestBody SysUserIdReq sysUserIdReq){
        try {
            SysUser sysUser = new SysUser();
/*            sysUser.setId(sysUserIdReq.getId());
            sysUser.setPassword(HappyPasswordEncoder.passwordEncryption("123456"));*/
            iSysUserService.updateById(sysUser);
        } catch (BusinessException e) {
            logger.info("SysUserController -> resetPassword() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysUserController -> resetPassword() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

}
